package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.MchntOrderInfoMapper;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.StatisTransAmt;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.DataBasePool;
import cc.rengu.utility.dbs.DataBasePoolConfig;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户订单表操作接口实现类
 * Created by 王成 on 2020/2/6
 */
public class MchntOrderInfoMapperImpl implements MchntOrderInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public MchntOrderInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertMchntOrderInfo(MchntOrderInfo mchntOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntOrderInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderInfo, null);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("登记商户订单信息失败,instId:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", mchntOrderInfo.getInstId(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getSysSeqNum(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntOrderInfoByPrimaryKey(MchntOrderInfo mchntOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntOrderInfoByPrimaryKey");
        mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderInfo, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntOrderInfoHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderInfo, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据主键更新商户订单信息失败,instId:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", mchntOrderInfo.getInstId(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getSysSeqNum(), iReturnCode);
            }
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntOrderStatusByPrimaryKey(MchntOrderInfo mchntOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntOrderStatusByPrimaryKey");
        mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderInfo, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntOrderStatusHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderInfo, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据主键更新商户订单状态失败,instId:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", mchntOrderInfo.getInstId(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getSysSeqNum(), iReturnCode);
            }
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntOrderInfoByMchntOrderId(MchntOrderInfo mchntOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntOrderInfoByMchntOrderId");
        mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderInfo, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntOrderInfoHisByMchntOrderId");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntOrderInfo, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据商户订单号更新商户订单信息失败,instId:<{}>,mchntOrderId:<{}>,mchntNo:<{}>,txnDate:<{}>,txnTime:<{}>,iReturnCode:<{}>", mchntOrderInfo.getInstId(), mchntOrderInfo.getMchntOrderId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getTxnDate(), mchntOrderInfo.getTxnTime(), iReturnCode);
            }
        }
        return iReturnCode;
    }

    @Override
    public MchntOrderInfo selectMchntOrderInfoByPrimaryKey(String instId, String sysSeqNum, String mchntNo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfoByPrimaryKey");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setSysSeqNum(sysSeqNum);
        mchntOrderInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntOrderInfo, mchntOrderInfo);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfoHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntOrderInfo, mchntOrderInfo);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.warn("根据主键查询商户订单信息失败,instId:<{}>,sysSeqNum:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, sysSeqNum, mchntNo, iReturnCode);
                return null;
            }
        }
        return mchntOrderInfo;
    }

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoBySysSeqNum(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfoBySysSeqNum");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setSysSeqNum(sysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null != objectList && objectList.isEmpty()) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfoHisBySysSeqNum");
            objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
            if (null == objectList || objectList.isEmpty()) {
                rglog.warn("根据系统流水号查询商户订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
                return null;
            }
        } else if (null == objectList) {
            rglog.warn("根据系统流水号查询商户订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoByMchntNo(String instId, String mchntNo, String beginDate, String endDate) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfoByMchntNo");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setMchntNo(mchntNo);
        mchntOrderInfo.setTxnDate(beginDate);
        mchntOrderInfo.setRemark1(endDate);
        List<MchntOrderInfo> mchntOrderInfoList = null;
        /* 查询当前表指定日期区间的交易流水 */
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null != objectList && !objectList.isEmpty()) {
            mchntOrderInfoList = objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
        }
        /* 查询历史表指定日期区间的交易流水 */
        sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfoHisByMchntNo");
        objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null != objectList && !objectList.isEmpty()) {
            if (null == mchntOrderInfoList) {
                mchntOrderInfoList = objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
            } else {
                mchntOrderInfoList.addAll(objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList()));
            }
        }
        if (null == mchntOrderInfoList || mchntOrderInfoList.isEmpty()) {
            rglog.warn("根据商户号查询商户订单信息失败,instId:<{}>,mchntNo:<{}>,beginDate:<{}>,endDate:<{}>", instId, mchntNo, beginDate, endDate);
            return null;
        }
        return mchntOrderInfoList;
    }

    @Override
    public List<StatisTransAmt> statisMchntTransAmtByTransType(String instId, String mchntNo, String txnDate) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "statisMchntTransAmtByTransType");
        if (DataBasePoolConfig.DB_TYPE_ORACLE == DataBasePool.getSessionType(this.dbPoolName)) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "statisOracleMchntTransAmtByTransType");
        }
        StatisTransAmt statisTransAmt = new StatisTransAmt();
        statisTransAmt.setInstId(instId);
        statisTransAmt.setMchntNo(mchntNo);
        statisTransAmt.setTxnDate(txnDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, statisTransAmt, StatisTransAmt.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据商户号及交易日期统计各交易类型日总金额失败,instId:<{}>,mchntNo:<{}>,txnDate:<{}>", instId, mchntNo, txnDate);
            return null;
        }
        return objectList.stream().map(item -> (StatisTransAmt) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoByOrigSysSeqNum(String instId, String origSysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfoByOrigSysSeqNum");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setOrigSysSeqNum(origSysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        if (null != objectList && objectList.isEmpty()) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntOrderInfoHisByOrigSysSeqNum");
            objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
            if (null == objectList || objectList.isEmpty()) {
                rglog.warn("根据原交易流水号查询商户订单信息失败,instId:<{}>,origSysSeqNum:<{}>", instId, origSysSeqNum);
                return null;
            }
        } else if (null == objectList) {
            rglog.warn("根据原交易流水号查询商户订单信息失败,instId:<{}>,origSysSeqNum:<{}>", instId, origSysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }
}
